﻿using HCQ2_IBLL;
using HCQ2_Model;
using HCQ2_Model.SchoolClass;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using HCQ2_Common;
using HCQ2_Model.ViewModel;

namespace HCQ2_BLL
{
    /// <summary>
    /// 班级
    /// </summary>
    public partial class T_ClassBLL: IT_ClassBLL
    {

        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public TableModel GetClassList(SchoolClassListQuery query)
        {
            return DBSession.IT_ClassDAL.GetClassList(query);
        }

        /// <summary>
        /// 获取学校下面用户用于设置班主任
        /// </summary>
        /// <param name="unit"></param>
        /// <returns></returns>
        public List<TeacherSelectList> ListTeacherSelect(string unit)
        {
            var result = DBSession.IT_UserDAL.ListTeacherSelect(unit);

            return result;
        }

        /// <summary>
        /// 新增或编辑班级信息，依据主键ID是否为空
        /// </summary>
        /// <param name="schoolClassEdit"></param>
        /// <returns></returns>
        public int SaveClass(SchoolClassEdit schoolClassEdit)
        {
            if (CheckClassName(schoolClassEdit.schoolName, schoolClassEdit.className, schoolClassEdit.id))
            {
                throw new BllMessageException(string.Format("学校：{0} 下面，已经存在班级：{1}", schoolClassEdit.schoolName, schoolClassEdit.className));
            }

            T_Class saveData = schoolClassEdit.MapTo<T_Class>();
            saveData.updateTime = DateTime.Now;
            saveData.createTime = DateTime.Now;
            saveData.createUser = saveData.updateUser;

            if (schoolClassEdit.id == null)
            {
                DBSession.IT_ClassDAL.Add(saveData);
                return saveData.id;
            }
            else {
                //编辑
                DBSession.IT_ClassDAL.Modify(saveData,w=>w.id==saveData.id, "schoolName", "schoolCode", "className", "entranceYear", "teacherName", "teacherUserId", "classStudentNum",
                    "remark", "updateTime", "updateUser");

                return saveData.id;
            }
        }

        /// <summary>
        /// 删除班级
        /// </summary>
        /// <param name="id">班级ID</param>
        /// <returns>null删除成功，不为空删除失败的错误信息</returns>
        public string DelClass(int id)
        {
            int studentCount = DBSession.IT_StudentDAL.SelectCount(w=>w.classId==id);
            if (studentCount > 0) {
                return string.Format("无法删除班级，班级下面还有：{0}个学生",studentCount); ;
            }

            DBSession.IT_ClassDAL.Delete(w=>w.id==id);
            return null;
        }

        /// <summary>
        /// 验证学校下面班级名称是否已经存在
        /// </summary>
        /// <param name="schoolName">学校名称</param>
        /// <param name="schoolClassName">班级名称</param>
        /// <param name="id">编辑时传入ID，验证是否是自己</param>
        /// <returns>true存在、false不存在</returns>
        private bool CheckClassName(string schoolName,string schoolClassName,Nullable<int> id) {
            T_Class cla = DBSession.IT_ClassDAL.FindSingle(w=>w.schoolName.Equals(schoolName) && w.className.Equals(schoolClassName));

            if (cla == null) {
                return false;
            }

            //编辑操作，查到自己
            if (id != null && cla.id == id)
            {
                return false;
            }
            return true;
        }
    }
}
